#! /bin/sh
#
# This script changes the file name extension for all C++ files
# from .cc to a different extension. This is useful for C++ compilers
# which do note recognise ".cc" as a valid extension for C++ code.
# For instance, the IBM xlC compiler on AIX 3.x requires ".C" and
# older versions of Microsoft Visual C++ need ".cpp" or ".cxx".
#
# The script must be called from the "dcmtk/" directory, e.g.
#   cd dcmtk
#   config/changext cpp
#
# The first command line argument (if given) overrides the
#   new extension (default: C).
#   You must not specify the dot preceding the extension.
#
# The second command line argument (if given) overrides the
#   old extension (default: cc). This is useful if you want
#   to "rename back".
#
# Author: Marco Eichelberg, (C) 1997-2011 Kuratorium OFFIS e.V.
#

newext=${1-C}
oldext=${2-cc}

echo "renaming C++ files from .$oldext to .$newext"
for file in `find . -name "*.$oldext" -print`
do
  newfile=`echo $file | sed -e "s/\.$oldext/\.$newext/g"`
  `mv $file $newfile`
done

if [ -f dcmjpls/apps/dcml2pnm.$newext ] ; then
  echo "updating implementation include in dcml2pnm.$newext"
  `cat dcmjpls/apps/dcml2pnm.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmimage\/apps\/dcm2pnm\.$oldext\"/#include \"..\/..\/dcmimage\/apps\/dcm2pnm.$newext\"/g" >dcmjpls/apps/dcml2pnm.new`
  `mv dcmjpls/apps/dcml2pnm.new dcmjpls/apps/dcml2pnm.$newext`
fi

if [ -f dcmjpeg/apps/dcmj2pnm.$newext ] ; then
  echo "updating implementation include in dcmj2pnm.$newext"
  `cat dcmjpeg/apps/dcmj2pnm.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmimage\/apps\/dcm2pnm\.$oldext\"/#include \"..\/..\/dcmimage\/apps\/dcm2pnm.$newext\"/g" >dcmjpeg/apps/dcmj2pnm.new`
  `mv dcmjpeg/apps/dcmj2pnm.new dcmjpeg/apps/dcmj2pnm.$newext`
fi

if [ -f dcmjpeg/apps/dcmmkdir.$newext ] ; then
  echo "updating implementation include in dcmmkdir.$newext"
  `cat dcmjpeg/apps/dcmmkdir.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmdata\/apps\/dcmgpdir\.$oldext\"/#include \"..\/..\/dcmdata\/apps\/dcmgpdir.$newext\"/g" >dcmjpeg/apps/dcmmkdir.new`
  `mv dcmjpeg/apps/dcmmkdir.new dcmjpeg/apps/dcmmkdir.$newext`
fi

if [ -f dcmeval/apps/tcpsrv_e.$newext ] ; then
  echo "updating implementation include in tcpsrv_e.$newext"
  `cat dcmeval/apps/tcpsrv_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmprscp\/apps\/tcpsrv\.$oldext\"/#include \"..\/..\/dcmprscp\/apps\/tcpsrv.$newext\"/g" >dcmeval/apps/tcpsrv_e.new`
  `mv dcmeval/apps/tcpsrv_e.new dcmeval/apps/tcpsrv_e.$newext`
fi

if [ -f dcmeval/apps/tcpprt_e.$newext ] ; then
  echo "updating implementation include in tcpprt_e.$newext"
  `cat dcmeval/apps/tcpprt_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmprint\/apps\/tcpprt\.$oldext\"/#include \"..\/..\/dcmprint\/apps\/tcpprt.$newext\"/g" >dcmeval/apps/tcpprt_e.new`
  `mv dcmeval/apps/tcpprt_e.new dcmeval/apps/tcpprt_e.$newext`
fi

if [ -f dcmeval/apps/dcmcjp2k_e.$newext ] ; then
  echo "updating implementation include in dcmcjp2k_e.$newext"
  `cat dcmeval/apps/dcmcjp2k_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmjp2k\/apps\/dcmcjp2k\.$oldext\"/#include \"..\/..\/dcmjp2k\/apps\/dcmcjp2k.$newext\"/g" >dcmeval/apps/dcmcjp2k_e.new`
  `mv dcmeval/apps/dcmcjp2k_e.new dcmeval/apps/dcmcjp2k_e.$newext`
fi

if [ -f dcmeval/apps/dcmdjp2k_e.$newext ] ; then
  echo "updating implementation include in dcmdjp2k_e.$newext"
  `cat dcmeval/apps/dcmdjp2k_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmjp2k\/apps\/dcmdjp2k\.$oldext\"/#include \"..\/..\/dcmjp2k\/apps\/dcmdjp2k.$newext\"/g" >dcmeval/apps/dcmdjp2k_e.new`
  `mv dcmeval/apps/dcmdjp2k_e.new dcmeval/apps/dcmdjp2k_e.$newext`
fi

if [ -f dcmqrdbx/apps/dcmqrscq.$newext ] ; then
  echo "updating implementation include in dcmqrscq.$newext"
  `cat dcmqrdbx/apps/dcmqrscq.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmqrdb\/apps\/dcmqrscp\.$oldext\"/#include \"..\/..\/dcmqrdb\/apps\/dcmqrscp.$newext\"/g" >dcmqrdbx/apps/dcmqrscq.new`
  `mv dcmqrdbx/apps/dcmqrscq.new dcmqrdbx/apps/dcmqrscq.$newext`
fi

if [ -f dcmeval/apps/ppsscpfs_e.$newext ] ; then
  echo "updating implementation include in ppsscpfs_e.$newext"
  `cat dcmeval/apps/ppsscpfs_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmpps\/apps\/ppsscpfs\.$oldext\"/#include \"..\/..\/dcmpps\/apps\/ppsscpfs.$newext\"/g" | sed -e "s/#include \"\.\.\/\.\.\/dcmpps\/apps\/ppscefs\.$oldext\"/#include \"..\/..\/dcmpps\/apps\/ppscefs.$newext\"/g" >dcmeval/apps/ppsscpfs_e.new`
  `mv dcmeval/apps/ppsscpfs_e.new dcmeval/apps/ppsscpfs_e.$newext`
fi

if [ -f dcmeval/apps/ppsmgr_e.$newext ] ; then
  echo "updating implementation include in ppsmgr_e.$newext"
  `cat dcmeval/apps/ppsmgr_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmpps\/apps\/ppsmgr\.$oldext\"/#include \"..\/..\/dcmpps\/apps\/ppsmgr.$newext\"/g" | sed -e "s/#include \"\.\.\/\.\.\/dcmpps\/apps\/ppsmgrce\.$oldext\"/#include \"..\/..\/dcmpps\/apps\/ppsmgrce.$newext\"/g" >dcmeval/apps/ppsmgr_e.new`
  `mv dcmeval/apps/ppsmgr_e.new dcmeval/apps/ppsmgr_e.$newext`
fi

if [ -f dcmeval/apps/pawscpfs_e.$newext ] ; then
  echo "updating implementation include in pawscpfs_e.$newext"
  `cat dcmeval/apps/pawscpfs_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmppswm\/apps\/pawscpfs\.$oldext\"/#include \"..\/..\/dcmppswm\/apps\/pawscpfs.$newext\"/g" | sed -e "s/#include \"\.\.\/\.\.\/dcmppswm\/apps\/pawcefs\.$oldext\"/#include \"..\/..\/dcmppswm\/apps\/pawcefs.$newext\"/g" >dcmeval/apps/pawscpfs_e.new`
  `mv dcmeval/apps/pawscpfs_e.new dcmeval/apps/pawscpfs_e.$newext`
fi

if [ -f dcmeval/apps/dcmanon_e.$newext ] ; then
  echo "updating implementation include in dcmanon_e.$newext"
  `cat dcmeval/apps/dcmanon_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmmisc\/apps\/dcmanon\.$oldext\"/#include \"..\/..\/dcmmisc\/apps\/dcmanon.$newext\"/g" >dcmeval/apps/dcmanon_e.new`
  `mv dcmeval/apps/dcmanon_e.new dcmeval/apps/dcmanon_e.$newext`
fi

if [ -f dcmmisc/apps/detecscp.$newext ] ; then
  echo "updating implementation include in detecscp.$newext"
  `cat dcmmisc/apps/detecscp.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmnet\/apps\/storescp\.$oldext\"/#include \"..\/..\/dcmnet\/apps\/storescp.$newext\"/g" >dcmmisc/apps/detecscp.new`
  `mv dcmmisc/apps/detecscp.new dcmmisc/apps/detecscp.$newext`
fi

if [ -f dcmmisc/apps/detecscu.$newext ] ; then
  echo "updating implementation include in detecscu.$newext"
  `cat dcmmisc/apps/detecscu.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmnet\/apps\/echoscu\.$oldext\"/#include \"..\/..\/dcmnet\/apps\/echoscu.$newext\"/g" >dcmmisc/apps/detecscu.new`
  `mv dcmmisc/apps/detecscu.new dcmmisc/apps/detecscu.$newext`
fi

if [ -f dcmeval/apps/mppsscu_e.$newext ] ; then
  echo "updating implementation include in mppsscu_e.$newext"
  `cat dcmeval/apps/mppsscu_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmppscu\/apps\/mppsscu\.$oldext\"/#include \"..\/..\/dcmppscu\/apps\/mppsscu.$newext\"/g" >dcmeval/apps/mppsscu_e.new`
  `mv dcmeval/apps/mppsscu_e.new dcmeval/apps/mppsscu_e.$newext`
fi

if [ -f dcmeval/apps/stcomscu_e.$newext ] ; then
  echo "updating implementation include in stcomscu_e.$newext"
  `cat dcmeval/apps/stcomscu_e.$newext | sed -e "s/#include \"\.\.\/\.\.\/dcmstcom\/apps\/stcomscu\.$oldext\"/#include \"..\/..\/dcmstcom\/apps\/stcomscu.$newext\"/g" >dcmeval/apps/stcomscu_e.new`
  `mv dcmeval/apps/stcomscu_e.new dcmeval/apps/stcomscu_e.$newext`
fi

echo "updating makefiles"
for file in `find . -name "Makefile*" -print`
do
  `cat $file | sed -e "s/\.$oldext/\.$newext/g" >$file.new`
  `mv $file.new $file`
done

if [ -f CMake/GenerateDCMTKConfigure.cmake ] ; then
  echo "updating CMake/GenerateDCMTKConfigure.cmake"
  cat CMake/GenerateDCMTKConfigure.cmake | sed -e "s/\.$oldext/.$newext/g" > CMake/GenerateDCMTKConfigure.cmake.new
  mv CMake/GenerateDCMTKConfigure.cmake.new CMake/GenerateDCMTKConfigure.cmake
fi

if [ -f CMake/dcmtkMacros.cmake ] ; then
  echo "updating CMake/dcmtkMacros.cmake"
  cat CMake/dcmtkMacros.cmake | sed -e "s/\.$oldext/.$newext/g" > CMake/dcmtkMacros.cmake.new
  mv CMake/dcmtkMacros.cmake.new CMake/dcmtkMacros.cmake
fi

echo "done."
